home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / mx / mx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-11  |  4.4 KB  |  190 lines

  1. /* 
  2.  * mx.c --
  3.  *
  4.  *    This file contains the top-level routines for "mx", a mouse-based
  5.  *    editor that runs on top of the X window system and uses the Sx
  6.  *    library package.
  7.  *
  8.  * Copyright (C) 1986, 1987, 1988 Regents of the University of California.
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #ifndef lint
  19. static char rcsid[] = "$Header: /sprite/src/cmds/mx/RCS/mx.c,v 1.27 90/11/11 11:52:00 ouster Exp $ SPRITE (Berkeley)";
  20. #endif not lint
  21.  
  22. #include <X11/Xlib.h>
  23. #include <X11/Xutil.h>
  24. #define Time SpriteTime
  25. #include <ctype.h>
  26. #include <errno.h>
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "mxInt.h"
  32.  
  33. /*
  34.  * Forward references to other procedures defined in this file:
  35.  */
  36.  
  37. static void        MainSignalProc();
  38.  
  39. /*
  40.  *----------------------------------------------------------------------
  41.  *
  42.  * main --
  43.  *
  44.  *    The main program for mx.  Initialize, load file, create window,
  45.  *    and loop processing events.
  46.  *
  47.  * Results:
  48.  *    None.
  49.  *
  50.  * Side effects:
  51.  *    Almost anything can happen.  Depends on commands that user types.
  52.  *
  53.  *----------------------------------------------------------------------
  54.  */
  55.  
  56. main(argc, argv)
  57.     int argc;            /* Count of command-line arguments. */
  58.     char **argv;        /* Array of strings containing args. */
  59. {
  60.     int detach = 0;
  61.     int signalPid = 0;
  62.     int i, put;
  63.     char *p;
  64.     Tcl_Interp *interp;        /* Initial interpreter used for error
  65.                  * reporting before there's a window open
  66.                  * within an interpreter of its own to use. */
  67.  
  68.     /*
  69.      * If running under Sprite, tell the memory allocator to be paranoid
  70.      * about freeing the same block twice.
  71.      */
  72.  
  73. #ifdef sprite
  74.     extern int memAllowFreeingFree;
  75.     memAllowFreeingFree = 0;
  76. #endif
  77.  
  78.     /*
  79.      *--------------------------------------------------------------
  80.      * Parse some of the command line arguments, leaving many others
  81.      * to get parsed by Mx_OpenCmd.
  82.      *--------------------------------------------------------------
  83.      */
  84.  
  85.     p = rindex(argv[0], '/');
  86.     if (p == NULL) {
  87.     p = argv[0];
  88.     } else {
  89.     p++;
  90.     }
  91.     if (strcmp(p, "mxsync") == 0) {
  92.     detach = 0;
  93.     } else {
  94.     detach = 1;
  95.     }
  96.     interp = Tcl_CreateInterp();
  97.     for (i = 1, put = 1; i < argc; i++) {
  98.     if (strcmp(argv[i], "-D") == 0) {
  99.         detach = 0;
  100.     } else if (strcmp(argv[i], "-s") == 0) {
  101.         i++;
  102.         if (i >= argc) {
  103.         fprintf(stderr,
  104.             "Mx: argument for \"%s\" switch missing.\n",
  105.             argv[i-1]);
  106.         exit(1);
  107.         }
  108.         signalPid = atoi(argv[i]);
  109.     } else {
  110.         argv[put] = argv[i];
  111.         put++;
  112.     }
  113.     }
  114.     argc = put;
  115.     if (detach) {
  116.     Proc_Detach(0);
  117.     }
  118.     Sx_SetErrorHandler();
  119.     (void) signal(SIGINT, MainSignalProc);
  120.     (void) signal(SIGQUIT, MainSignalProc);
  121.     (void) signal(SIGTERM, MainSignalProc);
  122.  
  123.     /*
  124.      *------------------------------------------------------
  125.      * Create the (first) window.
  126.      *------------------------------------------------------
  127.      */
  128.  
  129.     if (Mx_OpenCmd((MxWindow *) NULL, interp, argc, argv) != TCL_OK) {
  130.     fprintf(stderr, "Mx quitting: %s\n", interp->result);
  131.     exit(1);
  132.     }
  133.  
  134.     /*
  135.      *--------------------------------------------
  136.      * Enter a loop reading and processing events.
  137.      *--------------------------------------------
  138.      */
  139.  
  140.     while (1) {
  141.     XEvent event;
  142.     XNextEvent(mx_Display, &event);
  143.  
  144.     /*
  145.      * Part of Mike's hack.  Remove when Mike's gone.
  146.      */
  147.      
  148.     if ((signalPid != 0) && (event.type == Expose)) {
  149.         kill(signalPid, SIGINT);
  150.         signalPid = 0;
  151.     }
  152.     Sx_HandleEvent(&event);
  153.     if (mx_FileCount == 0) {
  154.         exit(0);
  155.     }
  156.     Mx_Update();
  157.     }
  158. }
  159.  
  160. /*
  161.  *----------------------------------------------------------------------
  162.  *
  163.  * MainSignalProc --
  164.  *
  165.  *    Invoked in response to fatal signals.
  166.  *
  167.  * Results:
  168.  *    None.
  169.  *
  170.  * Side effects:
  171.  *    Clean up Mx windows, then exit.
  172.  *
  173.  *----------------------------------------------------------------------
  174.  */
  175.  
  176.     /* ARGSUSED */
  177.  
  178. static void
  179. MainSignalProc(sigNum, sigCode)
  180.     int sigNum;            /* Signal number (major class). */
  181.     int sigCode;        /* Signal code (minor class). */
  182. {
  183.     static struct sigvec action = {SIG_DFL, 0, 0};
  184.     struct sigvec old;
  185.  
  186.     sigvec(sigNum, &action, &old);
  187.     Mx_Cleanup();
  188.     kill(getpid(), sigNum);
  189. }
  190.